package org.jeecg.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ccqtgb.sxp.partner.security.bo.RequestData;
import com.ccqtgb.sxp.partner.security.bo.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.api.utils.ApiDataUtils;
import org.jeecg.modules.api.utils.ValidatorUtils;
import org.jeecg.modules.api.vo.LeaseContractRestVo;
import org.jeecg.modules.api.vo.param.QueryLeaseContractParamVo;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.zcgl.entity.*;
import org.jeecg.modules.zcgl.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Api(tags = "三峡银行接口-合同信息")
@RestController
@RequestMapping("/api/bank/contract")
@Slf4j
public class leaseContractApiController {

    @Autowired
    private ILeaseContractInfoService iLeaseContractInfoService;
    @Autowired
    private ICustomerInfoService iCustomerInfoService;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private IAssetInfoService assetInfoService;
    @Autowired
    private IOrderPrestoreService orderPrestoreService;

    @ApiOperation(value = "获取合同信息", notes = "获取合同信息")
    @PostMapping("/getContractListById")
    public ResponseData<String> queryLeaseContractInfo(@RequestBody String reqBody) {

        RequestData<QueryLeaseContractParamVo> dataReslt = ApiDataUtils.getRequestData(reqBody, QueryLeaseContractParamVo.class);

        try {
            QueryLeaseContractParamVo vo = dataReslt.getBody();
            String validateResult = ValidatorUtils.validateFastStr(vo);
            if (validateResult != null) {
                return ApiDataUtils.getResponseData(Result.errorValidation(validateResult), dataReslt.getHead());
            }

            CustomerInfo customerInfo = iCustomerInfoService.getById(vo.getCustomerId());
            if (customerInfo == null) {
                return ApiDataUtils.getResponseData(Result.error("用户不存在!"), dataReslt.getHead());
            }


            SysDepart sysDepart = sysDepartService.selectCompanyByName(vo.getRecordedCompanyName());
            if (sysDepart == null) {
                return ApiDataUtils.getResponseData(Result.error("入账公司不存在!"), dataReslt.getHead());
            }

            //查询客合同
            LambdaQueryWrapper<LeaseContractInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(LeaseContractInfo::getCustomerId, vo.getCustomerId());
            queryWrapper.eq(LeaseContractInfo::getRentCompanyId, sysDepart.getId());
            List<LeaseContractInfo> leaseContractInfoList = iLeaseContractInfoService.list(queryWrapper);

            List<AssetInfo> assetInfoList = assetInfoService.list();

            List<LeaseContractRestVo> leaseContractRestVoList = new ArrayList<LeaseContractRestVo>();
            for (LeaseContractInfo l : leaseContractInfoList) {
                LeaseContractRestVo LeaseContractRestVo = new LeaseContractRestVo();
                LeaseContractRestVo.setContractNum(l.getContractNum());
                LeaseContractRestVo.setSite("");
                Optional<AssetInfo> optionalAssetInfo = assetInfoList.stream().filter(p -> p.getId().equals(l.getAssetId())).findAny();
                if (optionalAssetInfo.isPresent()) {
                    LeaseContractRestVo.setSite(optionalAssetInfo.get().getSite());
                }

                List<OrderPrestore> orderPrestoreList = orderPrestoreService.list(new LambdaQueryWrapper<OrderPrestore>().eq(OrderPrestore::getContractNum,l.getContractNum()).eq(OrderPrestore::getPayStatus,"30"));

                BigDecimal total = BigDecimal.ZERO;
                if (orderPrestoreList != null && orderPrestoreList.size() > 0) {
                    total = orderPrestoreList.stream().map(OrderPrestore::getAmountMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                }
                LeaseContractRestVo.setBalance(total);
                leaseContractRestVoList.add(LeaseContractRestVo);
            }
            return ApiDataUtils.getResponseData(Result.ok(leaseContractRestVoList), dataReslt.getHead());

        } catch (Exception e) {
            e.printStackTrace();
            return ApiDataUtils.getResponseData(Result.error(e.getMessage()), dataReslt.getHead());
        }

    }

}
